// pages/login/login.js
var app = getApp(), api = require('../../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: null,
    phoneNumber: null,
    identifyingCode: null,
    passWord: null,
    repassWord: null,
    btnType: 1,
    defaultWaitTime: 3,
    waitTime: null,
    defaultWaitText: "验证",
    waitInterval: null
  },
  onLoad: function (options) {
    
    var that = this;
    that.setData({
      userType: options.userType
    });
  },
  changeUserType: function (e) {
    var that = this;
    that.setData({
      userType: e.currentTarget.dataset.usertype
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "phoneNumber") {
      that.setData({
        phoneNumber: inputVlaue
      });
    }
    if (inputName == "identifyingCode") {
      that.setData({
        identifyingCode: inputVlaue
      });
    }
    if (inputName == "passWord") {
      that.setData({
        passWord: inputVlaue
      });
    }
    if (inputName == "repassWord") {
      that.setData({
        repassWord: inputVlaue
      });
    }
  },
  getIdentifyingCode: function (e) {

   
    var that = this;
    var reg = new RegExp('^[1][3,5,7,6,8][0-9]{9}$');
    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    that.setData({
      btnType: 0,
      waitTime: that.data.defaultWaitTime,
      waitInterval: setInterval(function () {
        if (that.data.waitTime <= 0) {
          clearInterval(that.data.waitInterval);
          that.setData({
            btnType: 1
          });
        }
        that.setData({
          waitTime: --that.data.waitTime
        });
      }, 1000)
    });
    //TODO 验证码获取
    app.request({
      url: api.default.checkcode,
      method: 'post',
      data: {
        bid:wx.getStorageSync('bid'),
        phone: that.data.phoneNumber
      },
      success: function (e) {
        that.setData({
          Smscode: e.code
        })

      }

    })
  },

  UserInfo: function (e) {
    wx.login({
      success:function(result){
        wx.request({
          url: api.default.user,
          method: "POST",
          data: {
            bid: wx.getStorageSync('bid'),
            utoken: '',
            code: result.code,
            encryptedData: e.encryptedData,
            iv: e.iv,
          },
          success: function (res) {
     
             if(res.data.success==10){
               wx.setStorageSync('openid', res.data.openid);
                wx.request({
                  url: api.default.userid,
                  method: "POST",
                  data: {
                    openid: res.data.openid
                  },
                  success: function (ress) {
                    wx.setStorageSync("uid", ress.data.data)
                    wx.setStorageSync("head", ress.data.head)
                    wx.setStorageSync("nickname", ress.data.nickname)
                  }
                })
             }
          }
        })
    
      }
    })
  
  },
  sendRequest: function (e) {
    
   
    
    var that = this;
    that.UserInfo(e.detail);
  
  
      
    

    if (that.validate()) {
      // TODO 发送请求


      app.request({
        url: api.default.userreg,
        method: 'post',
        data: {
          uid: wx.getStorageSync('uid'),
          phone: that.data.phoneNumber,
          password: that.data.passWord,
          type: this.data.userType,
          bid:wx.getStorageSync('bid')
        },
        success: function (e) {
          console.log(e);
          if (e.code == 1) {
            wx.showToast({
              title: e.msg,
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateTo({
                    url: '../login/index',
                  })
                }, 2000)
              }
            })
          } else {
            wx.showToast({
              title: e.msg,
              icon: 'none',
              mask: true,
            });
          }
        }
      })
    }
  },
  validate: function () {
    var that = this;
    var reg = new RegExp('^[1][3,5,7,6,8][0-9]{9}$');

    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    console.log(that.data.identifyingCode);
    if (!that.data.identifyingCode || that.data.identifyingCode.length <= 0) {
      wx.showToast({
        title: '验证码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (that.data.identifyingCode != that.data.Smscode) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.passWord || that.data.passWord.length <= 0) {
      wx.showToast({
        title: '密码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (that.data.repassWord != that.data.passWord) {
      wx.showToast({
        title: '确认密码有误',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  }
})